package com.supermarket.view;

import com.supermarket.Main;
import com.supermarket.entity.CommodityEntity;
import com.supermarket.serivce.CommoditySerivce;
import com.supermarket.utils.DateUtil;
import com.supermarket.utils.ScannerUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class CommodityView {
    private static CommoditySerivce CommoditySerivce = new CommoditySerivce();
    public void CommodityView(){
        while (true) {
            System.out.println("=======商品管理=======");
            System.out.println("1.查询所有的商品信息");
            System.out.println("2.根据id查询商品信息");
            System.out.println("3.新增商品信息");
            System.out.println("4.根据id修改商品信息");
            System.out.println("5.根据id删除商品信息");
            System.out.println("0. 返回上一级菜单");
            System.out.println("=======商品管理=======");
            System.out.println("请输入菜单序号：");
            int result = ScannerUtil.getInt();
            switch (result) {
                case 1:
                    showAll();
                    break;
                case 2:
                    findById();
                    break;
                case 3:
                    insert();
                    break;
                case 4:
                    updateId();
                    break;
                case 5:
                    delId();
                    break;
                case 0:
                    Main.mainMenu();
                default:
                    System.out.println("您输入的菜单序号不正确，请重新输入!");
                    break;
            }
        }
    }

    //展示全部
    public void showAll() {
        System.out.println("查询到的所有商品信息");
        ArrayList<CommodityEntity> CommodityEntities = CommoditySerivce.all();
        for (CommodityEntity stu : CommodityEntities) {
            System.out.println(stu);
        }
    }
    public void findById() {
        System.out.println("请输入商品的id：");
        Long stuId = ScannerUtil.getLong();

        CommodityEntity Commodity = CommoditySerivce.getById(stuId);
        if (Commodity == null) {
            System.out.println("该商品id" + Commodity + "不存在");
            return;
        }

        System.out.println("商品信息：" +Commodity);
    }
    //删除
    public void delId() {
        System.out.println("请输入商品的id：");
        Long stuId = ScannerUtil.getLong();
        int result = CommoditySerivce.del(stuId);
        if (result > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }
    //插入
    public void insert() {
        System.out.println("请输入商品的名称：");
        String name = ScannerUtil.getString();

        System.out.println("请输入商品的数量：");
        long number = ScannerUtil.getLong();

        System.out.println("请输入商品的进货价格：");
        Long enterPrice = ScannerUtil.getLong();
        System.out.println("请输入商品的销售价格：");
        Long sales = ScannerUtil.getLong();
        ScannerUtil.getLine();
        System.out.println("请输入商品的生产日期（格式为yyyy-MM-dd）:");
        String dateString = ScannerUtil.getLine();
        Date productionDate;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            productionDate = dateFormat.parse(dateString);
        } catch (ParseException e) {
            System.out.println("日期格式不正确，请使用yyyy-MM-dd格式");
            return;
        }
        String formattedProductionDate = dateFormat.format(productionDate);

        System.out.println("请输入商品的保质期（以天数计）：");
        long guaranteePeriod = ScannerUtil.getLong();

        ScannerUtil.getLine();
        System.out.println("请输入商品的供应商：");
        String supplier = ScannerUtil.getLine();

        String createTimeString = DateUtil.getNowTime();
        System.out.println("恭喜！该商品创建成功！创建时间为：" + createTimeString);

        CommodityEntity commodityEntity = new CommodityEntity(null,name, number, enterPrice,sales, formattedProductionDate, guaranteePeriod, supplier, createTimeString);
        int result = CommoditySerivce.insert(commodityEntity);
        if (result > 0) {
            System.out.println("插入商品信息成功");
        } else {
            System.out.println("插入商品信息失败");
        }
    }

    public void updateId() {
        System.out.println("请输入商品的id：");
        Long commodityId = ScannerUtil.getLong();
        CommodityEntity commodity = CommoditySerivce.getById(commodityId);
        if (commodity == null) {
            System.out.println("没有查询到该id对应的商品信息");
            return;
        }
        ScannerUtil.getLine();

        System.out.println("请输入商品的名称：");
        String name = ScannerUtil.getLine();

        System.out.println("请输入商品的数量：");
        Long number = ScannerUtil.getLong();

        System.out.println("请输入商品的进货价格：");
        Long enterPrice = ScannerUtil.getLong();

        System.out.println("请输入商品的销售价格：");
        Long sales = ScannerUtil.getLong();
        ScannerUtil.getLine();
        System.out.println("请输入商品的生产日期（格式为yyyy-MM-dd）:");
        String dateString = ScannerUtil.getLine();
        Date productionDate;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            productionDate = dateFormat.parse(dateString);
        } catch (ParseException e) {
            System.out.println("日期格式不正确，请使用yyyy-MM-dd格式");
            return;
        }
        String formattedProductionDate = dateFormat.format(productionDate);

        System.out.println("请输入商品的保质期：");
        Long guaranteePeriod = ScannerUtil.getLong();
        ScannerUtil.getLine();
        System.out.println("请输入商品的供应商：");
        String supplier = ScannerUtil.getLine();

        String createTimeString = DateUtil.getNowTime();
        System.out.println("恭喜！该商品创建成功！创建时间为：" + createTimeString);

        CommodityEntity studentEntity = new CommodityEntity(commodityId, name, number, enterPrice, sales, formattedProductionDate, guaranteePeriod, supplier,createTimeString);
        int result =CommoditySerivce.update(studentEntity);
        if (result > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }
}
